import { createRouter, createWebHashHistory } from "vue-router";
import { mainStore } from "../store/index.js";
import { ElNotification } from 'element-plus';
const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/",
            redirect: {
                name: "Login",
            }
        },
        {
            path: "/Login",
            name: "Login",
            component: () => import("../views/Login.vue"),
        },
        {
            path: "/Regist",
            name: "Regist",
            component: () => import("../views/Regist.vue"),
        },
        {
            path: "/Manager",
            name: "Manager",
            component: () => import("../views/Manager.vue"),
            children: [
                {
                    name: "DataView",
                    path: "DataView",
                    component: () => import("../views/DataView.vue"),
                    meta: {
                        title: "主页",
                        requireAuth: true
                    }
                },
                {
                    name: "addToShopCarResult",
                    path: "addToShopCarResult/:id",
                    component: () => import("../views/ShopCar/addToShopCarResult.vue"),
                    meta: {
                        title: "添加去购物车的结果",
                        requireAuth: true
                    }
                },
            ]
        }
        , {
            name: "myShopCarList",
            path: "/myShopCarList",
            component: () => import("../views/ShopCar/myShopCarList.vue"),
            meta: {
                title: "我的购物车",
                requireAuth: true
            }
        }, {
            name: "checkoutOrder",
            path: "/checkoutOrder/:Ids",
            component: () => import("../views/OrderInfo/checkoutOrder.vue"),
            meta: {
                title: "确认订单",
                requireAuth: true
            }
        }, {
            name: "goodsDetail",
            path: "/goodsDetail/:id",
            component: () => import("../views/GoodsInfo/goodsInfo.vue"),
            meta: {
                title: "物品详细信息",
            }
        }, {
            name: "confirmOrder",
            path: "/confirmOrder/:id",
            component: () => import("../views/OrderInfo/confirmOrder.vue"),
            meta: {
                title: "确认订单",
            }
        },
        {
            path: "/search",
            name: "search",
            component: () => import("../views/Search.vue"),
            meta: {
                title: '搜索'
            }
        },
        {
            name: "customCenter",
            path: "/customCenter",
            component: () => import("../views/CustomInfo/customCenter.vue"),
            meta: {
                title: "个人中心",
                requireAuth: true
            },
         
            children: [
              
                {
                    name: "customInfo",
                    path: "customInfo",
                    component: () => import("../views/CustomInfo/customInfo.vue"),
                    meta: {
                        title: "用户信息",
                        requireAuth: true
                    }
                },
                {
                    name: "orderInfoList",
                    path: "orderInfoList",
                    component: () => import("../views/CustomInfo/orderInfoList.vue"),
                    meta: {
                        title: "我的订单",
                        requireAuth: true
                    }
                },
                {
                    name: "addressList",
                    path: "addressList",
                    component: () => import("../views/CustomInfo/addressList.vue"),
                    meta: {
                        title: "我的收货地址",
                        requireAuth: true
                    }
                }
            ]
        }
    ]
})
// 导航守卫 (路由的钩子函数)
// beforeEach跳转页面之前
router.beforeEach((to, from, next) => {
    const store = mainStore();
    if (to.meta.requireAuth) {
        //需要登录以后才能进
        if (store.token) {
            //说明有token，放行
            next();
        } else {
            ElNotification({
                type: "warning",
                title: "系统提示",
                message: "您还没有登录，请选去登录"
            });
            next({
                name: "Login"
            });
        }
    } else {
        //不需要登录
        next();
    }
});
export default router;